package bookManage.util;

import java.io.File;
import java.util.Scanner;
import java.util.TreeSet;

import Util.TestUtil;
import bookManage.model.BookModel;
import bookManage.model.BorrowModel;

public class BookUtil {

	/**
	 * 存放图书集合
	 */
	public static TreeSet<BookModel> books = new TreeSet<BookModel>();

	/**
	 * 存放图书信息的文件
	 */
	static String path = TestUtil.creatPath("BookManage", "books");

	static Scanner sc = new Scanner(System.in);

	/**
	 * 查询所有图书信息
	 */
	public void selectAll() {
		for (BookModel book : books) {
			info(book);
		}
	}

	/**
	 * 根据图书ISBN码查找图书
	 * 
	 * @return
	 * @return
	 */
	public BookModel select(String isbn) {
		for (BookModel book : books) {
			if (book.getIsbn().equals(isbn)) {
				return book;
			}
		}
		return null;
	}

	/**
	 * 图书详细信息
	 * 
	 * @param book
	 */
	public void info(BookModel book) {
		System.out.println("-----------------------");
		System.out.println("ID：" + book.getId());
		System.out.println("ISBN码：" + book.getIsbn());
		System.out.println("书名:" + book.getName());
		System.out.println("借阅次数：" + book.getNum());
		System.out.println("-----------------------");
		System.out.println();
	}

	/**
	 * 添加图书信息
	 */
	public void add() {
		System.out.println("========添加图书=========");
		System.out.print("请输入图书编号：");
		int id = TestUtil.abnormal();
		System.out.print("请输入图书的ISBN码：");
		String isbn = sc.nextLine();
		System.out.print("请输入图书名：");
		String name = sc.nextLine();
		System.out.println("=======================");

		BookModel book = new BookModel();
		book.setId(id);
		book.setIsbn(isbn);
		book.setName(name);
		System.out.println("添加成功！");

		books.add(book);

		TestUtil.write(path, books);
	}

	/**
	 * 借阅图书
	 */
	public void borrowBook() {
		selectAll();
		System.out.println("请输入您要借阅的图书ISBN码：");
		String isbn = sc.nextLine();
		BookModel book = select(isbn);
		if (book == null) {
			System.out.println("没有找到相关图书！");
			return;
		}
		book.setNum();

		BorrowModel borrowModel = new BorrowModel();// 添加借阅记录
		borrowModel.setBookIsbn(isbn);
		borrowModel.setBorrowCode();
		borrowModel.setBorrowTime();
		borrowModel.setCardId(CardUtil.user);

		BorrowUtil.borrows.add(borrowModel);

		TestUtil.write(BorrowUtil.path, BorrowUtil.borrows);

		System.out.println("借阅成功！");

		TestUtil.write(path, books);
	}

	public void returnBook() {
		BorrowUtil borrowUtil = new BorrowUtil();
		borrowUtil.selectByUser();
		System.out.println("请输入要归还的借阅编号：");
		int borrCode = TestUtil.abnormal();

		for (BorrowModel borrowModel : BorrowUtil.borrows) {
			if (borrowModel.getBorrowCode() == borrCode) {
				borrowModel.setReturnTime();
				TestUtil.write(BorrowUtil.path, BorrowUtil.borrows);
			}
		}
	}

	/**
	 * 初始化
	 */
	public static void init() {
		BookModel book = new BookModel();
		book.setId(10001);
		book.setIsbn("21001");
		book.setName("三国演义");

		books.add(book);

		TestUtil.write(path, books);

	}

	/**
	 * 读取文件中的内容
	 * 
	 * 反序列化
	 */
	static {
		File f = new File(path);
		if (f.length() == 0) {
			init();
		}
		books = TestUtil.read(path);

	}
}
